using System;
using System.Collections.Generic;
using System.Text;
using Safira.Tools.Sharepoint.Spome;
using System.Globalization;

namespace Safira.Sharepoint.Spome.Cli
{
    public sealed class Program
    {
        /// <summary>
        /// This avoids code analisys CA1053
        /// </summary>
        private Program() { }

        [System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity" )]
        public static void Main( string[] args )
        {
            ESPWeb mySPWeb = null;

            if (args.Length < 1)
            {
                #region Write Options
                
                Console.WriteLine( "HELP\n" );
                Console.WriteLine( "CSPOME -i\t\t\t Import WebSite" );
                Console.WriteLine( "CSPOME -e\t\t\t Export WebSite" );
                Console.WriteLine( "CSPOME -a\t\t\t Aprove List items" );
                Console.WriteLine( "CSPOME -A\t\t\t Approve All WebSite" );
                Console.WriteLine( "CSPOME -p\t\t\t Import/Export PageLayout association to ContentType" );
                Console.WriteLine( "CSPOME -r\t\t\t Reset Website Hierarchy Permissions" );
                Console.WriteLine( "CSPOME -W\t\t\t Rebuilds the publishing workflow" );
                Console.WriteLine( "CSPOME -v\t\t\t Copy sites with variation information" );
                Console.WriteLine( "CSPOME -f\t\t\t Upload/Download file from List" );
                return;

                #endregion
            }

            try
            {
                char option = Convert.ToChar(args[0][1]);
                switch (option)
                {
                    case 'i':
                        #region Import Web

                        if (args.Length != 3 && args.Length != 4)
                        {
                            Console.WriteLine("To import the xml version to the website type:");
                            Console.WriteLine("CSPOME -i http://SiteUrl originFile.xml [excludePages]");
                            return;
                        }
                        
                        if (args.Length == 4 && (String.Compare( args[3], "excludepages", true, CultureInfo.InvariantCulture ) == 0) )
                        {
                        mySPWeb = new ESPWeb(args[1]);
                            mySPWeb.Import(args[2], true);
                        break;

                        }
                        else
                        {
                            mySPWeb = new ESPWeb(args[1]);
                            mySPWeb.Import(args[2], false);
                            break;
                        }
                        #endregion

                    case 'e':
                        #region Export Web

                        if (args.Length != 3)
                        {
                            Console.WriteLine("To export the website to an xml version type:");
                            Console.WriteLine("CSPOME -e http://SiteUrl originFile.xml");
                            return;
                        }

                        mySPWeb = new ESPWeb(args[1]);
                        mySPWeb.Export(args[2]);
                        break;

                        #endregion

                    case 'a':
                        #region Approve All Elements In One List

                        if (args.Length < 3)
                        {
                            Console.WriteLine("To aprove all elements in one list type:");
                            Console.WriteLine("CSPOME -a http://SiteUrl listname");
                            return;
                        }
                        mySPWeb = new ESPWeb(args[1]);
                        string listName = "";

                        for (int i = 2; i < args.Length; i++)
                        {
                            listName = String.Format( CultureInfo.InvariantCulture, "{0}{1} ", listName, args[ i ] );
                        }

                        listName = listName.Trim();
                        mySPWeb.ApproveList(listName);
                        break;

                        #endregion

                    case 'A':
                        #region Approve All Pages In a Website

                        if (args.Length != 2)
                        {
                            Console.WriteLine("To aprove all elements in the WebSite");
                            Console.WriteLine("CSPOME -A http://SiteUrl");
                            return;
                        }

                        mySPWeb = new ESPWeb(args[1]);
                        mySPWeb.ApproveAllPages();
                        break;

                        #endregion

                    case 'p':
                        #region Page Layouts Options

                        if (args.Length < 4)
                        {
                            Console.WriteLine("To Import and Export PageLayouts association to ContentTypes");
                            Console.WriteLine("To Export:");
                            Console.WriteLine("\tSPOME -p -e http://SiteUrl filename.xml");
                            Console.WriteLine("To Import:");
                            Console.WriteLine("\tSPOME -p -i http://SiteUrl filename.xml");
                            return;
                        }
                        char option2 = args[1][1];
                        mySPWeb = new ESPWeb(args[2]);

                        switch (option2)
                        {
                            case 'e':
                                mySPWeb.ExportPageLayouts(args[3]);
                                break;
                            case 'i':
                                mySPWeb.ImportPageLayouts(args[3]);
                                break;
                            default:
                                Console.WriteLine("To Import and Export PageLayouts association to ContentTypes");
                                Console.WriteLine("To Export:");
                                Console.WriteLine("\tSPOME -p -e http://SiteUrl filename.xml");
                                Console.WriteLine("To Import:");
                                Console.WriteLine("\tSPOME -p -i http://SiteUrl filename.xml");
                                return;
                        }
                        break;

                        #endregion;

                    case 'c':
                        #region Export Content Types Web

                        if (args.Length < 4)
                        {
                            Console.WriteLine("To Import and Export Content Types association to Web");
                            Console.WriteLine("To Export:");
                            Console.WriteLine("\tSPOME -c -e http://SiteUrl filename.xml");
                            Console.WriteLine("To Import:");
                            Console.WriteLine("\tSPOME -c -i http://SiteUrl filename.xml");
                            Console.WriteLine( "To Get Statistics:" );
                            Console.WriteLine( "\tSPOME -c -s http://SiteUrl filename.xml" );
                            return;
                        }
                        char option3 = args[1][1];
                        mySPWeb = new ESPWeb(args[2]);
                        switch (option3)
                        {
                            case 'e':
                                mySPWeb.ExportContentTypes( args[3] );
                                break;
                            case 'i':
                                mySPWeb.ImportContentTypes( args[3] );
                                break;
                            case 's':
                                mySPWeb.ContentTypesStats( args[ 3 ] );
                                break;
                            default:
                                Console.WriteLine("To Import and Export Content Types association to Web");
                                Console.WriteLine("To Export:");
                                Console.WriteLine("\tSPOME -c -e http://SiteUrl filename.xml");
                                Console.WriteLine("To Import:");
                                Console.WriteLine("\tSPOME -c -i http://SiteUrl filename.xml");
                                return;
                        }
                        break;
                        #endregion;

                    case 'P':
                        #region Correct Permissions associated to Content types


                        #endregion
                        break;

                    case 'L':
                        #region Correct Page Layouts List
                        if (args.Length < 2)
                        {
                            Console.WriteLine("To correct the page layouts list");
                            Console.WriteLine("\tSPOME -L http://SiteUrl");
                            return;
                        }

                        mySPWeb = new ESPWeb(args[1]);

                        mySPWeb.RebuildPageLayoutsList();

                        #endregion
                        break;

                    case 'W':
                        #region Correct approval workflow
                        
                        if (args.Length < 3)
                        {
                            Console.WriteLine("To correct the page list parallel approval workflow");
                            Console.WriteLine("\tSPOME -W http://SiteCollectionUrl http://SiteWithFunctioningWorkflow");
                            return;
                        }

                        mySPWeb = new ESPWeb(args[1]);

                        mySPWeb.RebuildApprovalWorkflowInPages(args[2]);

                        #endregion
                        break;

                    case 'r':
                        #region Reset Website Permissions
                        if (args.Length < 2)
                        {
                            Console.WriteLine("To reset the permissions of a website");
                            Console.WriteLine("\tSPOME -r http://Websiteurl");
                            return;
                        }
                        ESPWeb.ResetPermissions(args[1]);

                        #endregion
                        break;

                    case 'v':
                        #region Copy sites with variations

                        /*
                        if (args.Length < 4)
                        {
                            Console.WriteLine("To copy content between web applications with variations");
                            Console.WriteLine("On source site:");
                            Console.WriteLine("\tSPOME -v -e http://OrigWebSiteSourceVariationUrl PathToExport Recursive(true/false)");
                            Console.WriteLine("On destination site:");
                            Console.WriteLine("\tSPOME -v -i http://DestWebSiteSourceVariationUrl PathToImport");
                            Console.WriteLine("This tool should be run by a site collection administrator");
                            return;
                        }

                        if (args[1][1].Equals('e'))
                        {
                            // By default, the export is not recursive
                            bool bRecursive = false;

                            // Check if the parameter Recursive is being used
                            try
                            {
                                bRecursive = Convert.ToBoolean(args[4]);
                            }
                            catch
                            { 
                                // Do nothing, use the sRecursive value by default
                            }
                            mySPWeb = new ESPWeb(args[2]);
                            mySPWeb.ExportSiteWithVariations(args[3], bRecursive);
                        }
                        else if (args[1][1].Equals('i'))
                        {
                            mySPWeb = new ESPWeb(args[3]);
                            mySPWeb.ImportSiteWithVariations(args[4]);
                        }
                        else 
                        {
                            Console.WriteLine("To copy content between web applications with variations");
                            Console.WriteLine("On source site:");
                            Console.WriteLine("\tSPOME -v -e http://OrigWebSiteSourceVariationUrl PathToExport Recursive(true/false)");
                            Console.WriteLine("On destination site:");
                            Console.WriteLine("\tSPOME -v -i http://DestWebSiteSourceVariationUrl PathToImport");
                            Console.WriteLine("This tool should be run by a site collection administrator");
                            return;                        
                        }

                        */
                        #endregion
                        break;

                    case 'f':
                        #region Upload/Download file from List

                        if ( args.Length < 5 )
                        {
                            Console.WriteLine( "To Upload / Download file from List" );
                            Console.WriteLine( "To Upload:" );
                            Console.WriteLine( "\tSPOME -f -u http://SiteUrl folder list filter" );
                            Console.WriteLine( "To Download:" );
                            Console.WriteLine( "\tSPOME -f -d http://SiteUrl folder list filter" );
                            return;
                        }
                        char option4 = args[ 1 ][ 1 ];
                        string filter = null;
                        mySPWeb = new ESPWeb( args[ 2 ] );

                        if ( args.Length > 5 )
                            filter = args[ 5 ];

                        switch ( option4 )
                        {
                            case 'u':
                                mySPWeb.UploadFilesToList( args[ 3 ], args[ 4 ], filter );
                                break;
                            case 'd':
                                mySPWeb.DownloadFilesFromList( args[ 3 ], args[ 4 ], filter );
                                break;
                            default:
                                Console.WriteLine( "To Upload / Download file from List" );
                                Console.WriteLine( "To Upload:" );
                                Console.WriteLine( "\tSPOME -f -u http://SiteUrl folder list" );
                                Console.WriteLine( "To Download:" );
                                Console.WriteLine( "\tSPOME -f -d http://SiteUrl folder list" );
                                return;
                        }
                        #endregion
                        
                        break;

                    default:
                        Console.WriteLine("Unknown Command. For Help Type: SPOME");
                        break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Fatal Error: {0}", e.Message);
            }
        }
    }
}
